import { createStore } from 'vuex';

const store = createStore({
    state() {
        return {
            user: {
                id: null, 
                name: '', 
            },
            merchant: {
                id: null, 
                name: '', 
            }
        };
    },
    mutations: {
        setUser(state, user) {
            state.user.id = user.id;
            state.user.name = user.name;
        },
        setMerchant(state, merchant) {
            state.merchant.id = merchant.id;
            state.merchant.name = merchant.name;
        },
        clearUser(state) {
            state.user.id = null;
            state.user.name = '';
        },
        clearMerchant(state) {
            state.merchant.id = null;
            state.merchant.name = '';
        }
    },
    actions: {
        login({ commit }, payload) {
            const { role, data } = payload;  // 将 payload 解构为 role 和 data
            switch (role) {
                case 'user':
                    commit('setUser', data);
                    break;
                case 'merchant':
                    commit('setMerchant', data);
                    break;
                default:
                    break;
            }
        },
        logout({ commit }, role) {
            switch (role) {
                case 'user':
                    commit('clearUser');
                    break;
                case 'merchant':
                    commit('clearMerchant');
                    break;
                default:
                    break;
            }
        }
    }
});

export default store;
